import { createRouter, createWebHistory } from 'vue-router'
import pinia from '../pinia/index'
import { useRouteStore } from '@/pinia/modules/routeCache'
import { loginStore } from '@/pinia/modules/login'
const routeStore = useRouteStore(pinia)
let routes = []

// 批量导入modules文件夹路由
const files = import.meta.globEager('./*/*.js')
for (const key in files) {
  if (Object.prototype.hasOwnProperty.call(files, key)) {
    routes = [...routes, ...files[key].default]
  }
}

// 处理是否需要缓存的页面
const keepAliveViews = []
const notKeepAliveViews = []
;(function getCache (routes) {
  // eslint-disable-next-line array-callback-return
  routes.map((route) => {
    if (route?.meta?.keepAlive) {
      keepAliveViews.push(route.name)
    } else if (route?.name) {
      notKeepAliveViews.push(route.name)
    }
    if (route?.children) {
      getCache(route.children)
    }
  })
})(routes)

console.log(keepAliveViews, notKeepAliveViews)
routeStore.setKeepAliveViews(keepAliveViews)
routeStore.setNotKeepAliveViews(notKeepAliveViews)

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior (to, from, savedPosition) {
    // keep-alive 返回缓存页面后记录浏览位置
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        /*  console.log(to.meta.top) */
        resolve({ left: 0, top: to.meta.top || 0 })
      }, 300)
    })
  }
})

router.beforeEach((to, from, next) => {
  document.title = to.meta.title || ''
  // 登录状态判断
  const login = loginStore()
  const curve = login.loginData.expires < Date.now()
  // console.log(curve, to.meta.requiresAuth, to.path !== '/login')
  if (curve && to.meta.requiresAuth && to.path === '/login') {
    login.setRedirect(to.path)
    next('/login')
  } else {
    next()
  }
})

router.afterEach((to, from, next) => {
  if (from.meta.keepAlive) {
    from.meta.top = document.documentElement.scrollTop
    console.log(from.meta.top)
  }
})

export default router
/* router.beforeEach((to, from, next) => {
    const appStore = useAppStore()
    document.title = (to.meta && t(to.meta.title)) || ''
    // 设置面包屑
    const breadCrumbList = []
    to.matched.forEach(item => {
        breadCrumbList.push({ name: item.meta.title, path: item.path })
    })
    appStore.setBreadCrumb(breadCrumbList)
    const jwt = sessionStorage.getItem('jwt') || ''

    if (to.path === '/login') {
        // 如果是登录状态 跳转到主页
        jwt ? next('/') : next()
    } else {
        if (to.meta && to.meta.whiteList) {
            next()
        }
        if (from.name === 'Login' && !jwt) {
            next(false)
            return false
        }
        if (jwt) {
            if (Object.prototype.hasOwnProperty.call(to.meta, 'roles')) {
                const roles = to.meta.roles || []
                const { role } = jwt && JSON.parse(decode(jwt))
                roles.includes(role) ? next() : next('/error')
                return false
            }
            next()
        } else {
            next({ path: '/login' })
        }
    }
})
 */
